package com.ksyun.campus.metaserver.config.metaserverconfig;

import com.ksyun.campus.metaserver.domain.MetaServerInfo;
import com.ksyun.campus.metaserver.domain.StatInfo;
import com.ksyun.campus.metaserver.services.DefaultDataServerPicker;
import com.ksyun.campus.metaserver.services.IDataServerPicker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author sxt
 * @version 1.0
 * @description TODO
 * @date 2023/8/11 01:22
 */
@Configuration
public class MetaServerConfig {
    @Resource MetaServerConfigProperties metaServerConfigProperties;
    @Bean
    public IDataServerPicker dataServerPicker() {
        return new DefaultDataServerPicker();
    }
    @Bean(name = "StatInfoMap")
    public Map<String, StatInfo> statInfoMap(){
        return new ConcurrentHashMap<>();
    }

    @Bean
    public MetaServerInfo metaServerInfo(){
        return new MetaServerInfo();
    }
    @PostConstruct
    private void prepareFilePath() {
        File file = new File(metaServerConfigProperties.getDataPath());
        if (!file.exists()) {
            boolean rs = file.mkdirs();
            Assert.isTrue(rs, "数据保存路径创建异常, 请检查!");
//            file = new File(metaServerConfigProperties.getDataPath()+);
//            try {
//                rs = file.createNewFile();
//                Assert.isTrue(rs, "服务recovery文件创建异常, 请检查!");
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
        }
    }
}
